home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / util / cli / clip.lha / clip / clip.c next >
Encoding:
C/C++ Source or Header  |  2000-07-30  |  6.6 KB  |  208 lines

  1. #define __USE_SYSBASE
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <proto/dos.h>
  5. #include <proto/exec.h>
  6. #include <exec/types.h>
  7. #include <exec/memory.h>
  8. #include <proto/iffparse.h>
  9.  
  10. static const char Version[]="$VER: Clip 1.0 (30.07.00)";
  11.  
  12. #define ID_FTXT MAKE_ID('F','T','X','T')
  13. #define ID_CHRS MAKE_ID('C','H','R','S')
  14.  
  15. #define OUTPUTSIZE 256
  16. #define BUFFERSIZE 4096
  17.  
  18. typedef struct {
  19.     BPTR    Input;
  20.     BPTR    Output;
  21.     BPTR    StdIn;
  22.     BPTR    StdOut;
  23.  
  24.     STRPTR    Buffer;
  25.     ULONG    BufLen;
  26.     STRPTR    File;
  27.     STRPTR    String;
  28.     ULONG    ClipUnit;
  29.  
  30.     BOOL    Store;
  31.     BOOL    IsStdIn;
  32.     BOOL    IsStdOut;
  33. } MyArgs;
  34.  
  35.  
  36. static int myfprintf(BPTR file, char *ctl, ...)
  37. {
  38.     long *arg1;
  39.     char *buffer;
  40.     ULONG buflen;
  41.  
  42.     /* Try to be as safe as possible without too much hassle */
  43.     buflen = strlen(ctl)+1024;
  44.     if (buffer = AllocMem(buflen, MEMF_ANY))
  45.     {
  46.         arg1 = (long *)(&ctl + 1);
  47.         RawDoFmt(ctl, arg1, (void (*))"\x16\xc0\x4e\x75", buffer);
  48.         Write(file, buffer, strlen(buffer));
  49.  
  50.         FreeMem(buffer, buflen);
  51.     }
  52.  
  53.     return 0;
  54. }
  55.  
  56. int main(int argc, char *argv[])
  57. {
  58.     struct Library *IFFParseBase = NULL;
  59.     struct ContextNode *cn;    
  60.     struct IFFHandle *iffh = NULL;
  61.     struct ClipboardHandle *ClipHandle = NULL;
  62.  
  63.     BPTR StdErr;
  64.     MyArgs *args = NULL;
  65.     struct RDArgs *rdargs = NULL;
  66.     STRPTR Template = "FILE,U=UNIT/K/N,STR=STRING/K,STORE/S,HELP/S";
  67.     enum { TEM_FILE, TEM_UNIT, TEM_STRING, TEM_STORE, TEM_HELP, TEM_NUMARGS };
  68.     LONG ArgArray[TEM_NUMARGS];
  69.     LONG len = 0, i;
  70.  
  71.     StdErr = Open("CONSOLE:", MODE_NEWFILE);
  72.     if (!(args = AllocMem(sizeof(MyArgs), MEMF_ANY | MEMF_CLEAR))) { myfprintf(StdErr, "Unable to allocate needed memory!\n"); goto exit; }
  73.  
  74.     args->StdIn = Input();
  75.     args->StdOut = Output();
  76.  
  77.     if (IsInteractive(args->StdIn)) args->IsStdIn = TRUE;
  78.     if (IsInteractive(args->StdOut)) args->IsStdOut = TRUE;
  79.  
  80.     for (i=0;i<TEM_NUMARGS;ArgArray[i++]=NULL);
  81.     rdargs = ReadArgs(Template,ArgArray,NULL);
  82.  
  83.     if (ArgArray[TEM_HELP] || !rdargs)
  84.     {
  85.         myfprintf(StdErr,"%s by Sigbjørn (CISC) Skjæret\n\n", &Version[6]);
  86.         myfprintf(StdErr,"Usage: %s ([<file>] | [STRING=<string>]) [UNIT=<unit>] [STORE]\n", argv[0]);
  87.         goto exit;
  88.     }
  89.     if (ArgArray[TEM_FILE]) args->File = (STRPTR)ArgArray[TEM_FILE];
  90.     if (ArgArray[TEM_STRING] && !args->File && args->IsStdIn) args->String = (STRPTR)ArgArray[TEM_STRING];
  91.     if (ArgArray[TEM_STORE] || !args->IsStdIn || args->String) args->Store = TRUE;
  92.     if (ArgArray[TEM_UNIT]) args->ClipUnit = *((ULONG *)ArgArray[TEM_UNIT]);
  93.     if (args->ClipUnit>255) { myfprintf(StdErr, "Illegal unit number: %ld!\n", args->ClipUnit); goto exit; }
  94.  
  95.     if (args->Store)
  96.     {
  97.         if (!args->IsStdIn)
  98.         {
  99.             args->Input = args->StdIn;
  100.             if (args->File)
  101.             {
  102.                 if (!(args->Output = Open(args->File, MODE_NEWFILE))) { myfprintf(StdErr, "Unable to open file \"%s\".\n", args->File); goto exit; }
  103.             }
  104.         }
  105.         else if (args->File)
  106.         {
  107.             if (!(args->Input = Open(args->File, MODE_OLDFILE))) { myfprintf(StdErr, "Unable to open file \"%s\".\n", args->File); goto exit; }
  108.         }
  109.         else args->Input = args->StdIn;
  110.     } else {
  111.         if (args->File)
  112.         {
  113.             if (!(args->Output = Open(args->File, MODE_NEWFILE))) { myfprintf(StdErr, "Unable to open file\"%s\".\n", args->File); goto exit; }
  114.         }
  115.         else args->Output = args->StdOut;
  116.     }
  117.  
  118.     if (!(IFFParseBase = OpenLibrary("iffparse.library",39))) { myfprintf(StdErr, "Couldn't open iffparse.library v39+!\n"); goto exit; }
  119.     if (!(iffh = AllocIFF())) { myfprintf(StdErr, "Unable to allocate IFF structure!\n"); goto exit; }
  120.     InitIFFasClip(iffh);
  121.     if (!(ClipHandle = OpenClipboard(args->ClipUnit))) { myfprintf(StdErr, "Couldn't open clipboard.device!\n"); goto exit; }
  122.     iffh->iff_Stream = (ULONG)ClipHandle;
  123.  
  124.     if (args->Store)
  125.     {
  126.         if (OpenIFF(iffh, IFFF_WRITE)) { myfprintf(StdErr, "Couldn't open clipboard for write.\n"); goto exit; }
  127.         if (PushChunk(iffh, ID_FTXT, ID_FORM, IFFSIZE_UNKNOWN)) { myfprintf(StdErr, "Unable to write to clipboard.\n"); goto exit; }
  128.         if (PushChunk(iffh, ID_FTXT, ID_CHRS, IFFSIZE_UNKNOWN)) { myfprintf(StdErr, "Unable to write to clipboard.\n"); goto exit; }
  129.  
  130.         if (args->String)
  131.         {
  132.             i = strlen(args->String);
  133.             if ((len = WriteChunkBytes(iffh, args->String, i)) < 0) { myfprintf(StdErr, "Unable to write to clipboard.\n"); goto exit; }
  134.             if (args->Output) Write(args->Output, args->String, i);
  135.             if (!args->IsStdOut) Write(args->StdOut, args->String, i);
  136.         } else {
  137.             if (!(args->Buffer = AllocMem((args->BufLen = BUFFERSIZE), MEMF_ANY))) { myfprintf(StdErr, "Unable to allocate needed memory!\n"); goto exit; }
  138.             while ((i = Read(args->Input, args->Buffer, args->BufLen)) > 0)
  139.             {
  140.                 if ((len += WriteChunkBytes(iffh, args->Buffer, i)) < 0) { myfprintf(StdErr, "Unable to write to clipboard.\n"); goto exit; }
  141.                 if (args->Output) Write(args->Output, args->Buffer, i);
  142.                 if (!args->IsStdOut) Write(args->StdOut, args->Buffer, i);
  143.             }
  144.         }
  145.         if (PopChunk(iffh)) { myfprintf(StdErr, "Unable to finalize clipboard entry.\n"); goto exit; }
  146.     } else {
  147.         if (OpenIFF(iffh, IFFF_READ)) { myfprintf(StdErr, "Couldn't open clipboard for read."); goto exit; }
  148.         if (StopChunk(iffh, ID_FTXT, ID_CHRS)) { myfprintf(StdErr, "Unable to register IFF chunk.\n"); goto exit; }
  149.  
  150.         if (ParseIFF(iffh, IFFPARSE_SCAN)) { myfprintf(StdErr, "Clipboard unit %ld is empty or contains invalid data.\n", args->ClipUnit); goto exit; }
  151.         if (!(cn = CurrentChunk(iffh))) { myfprintf(StdErr, "Clipboard unit %ld contains invalid data.\n", args->ClipUnit); goto exit; }
  152.  
  153.         if ((cn->cn_Type == ID_FTXT) && (cn->cn_ID == ID_CHRS))
  154.         {
  155.             if (!(args->Buffer = AllocMem((args->BufLen = cn->cn_Size+1), MEMF_ANY))) { myfprintf(StdErr, "Unable to allocate needed memory!\n"); goto exit; }
  156.             if ((len = ReadChunkBytes(iffh, args->Buffer, cn->cn_Size)) < 0) { myfprintf(StdErr, "Unable to read from clipboard.\n"); goto exit; }
  157.  
  158.             if (!args->File && args->IsStdOut && args->Buffer[len-1] != 0x0A) args->Buffer[len++] = 0x0A;
  159.  
  160.             if (args->File || !args->IsStdOut)
  161.             {
  162.                 if (args->File && !args->IsStdOut) Write(args->StdOut, args->Buffer, len);
  163.                 if (args->Output) Write(args->Output, args->Buffer, len);
  164.             } else if (args->Output) {
  165.                 for (i=0;i<=len-OUTPUTSIZE;i+=OUTPUTSIZE)
  166.                 {
  167.                     if (SetSignal(0,0) & SIGBREAKF_CTRL_C) goto exit;
  168.                     Write(args->Output, args->Buffer+i, OUTPUTSIZE);
  169.                 }
  170.                 if (i < len) Write(args->Output, args->Buffer+i, len-i);
  171.             }
  172.         }
  173.     }
  174.  
  175. exit:
  176.     if (args)
  177.     {
  178.         if (args->Input && (args->Input != args->StdIn))
  179.             Close(args->Input);
  180.  
  181.         if (args->Output && (args->Output != args->StdOut))
  182.             Close(args->Output);
  183.  
  184.         if (args->Buffer)
  185.             FreeMem(args->Buffer, args->BufLen);
  186.  
  187.         FreeMem(args, sizeof(MyArgs));
  188.     }
  189.  
  190.     if (iffh)
  191.     {
  192.         CloseIFF(iffh);
  193.  
  194.         if (ClipHandle)
  195.             CloseClipboard(ClipHandle);
  196.  
  197.         FreeIFF(iffh);
  198.     }
  199.     if (IFFParseBase)
  200.         CloseLibrary(IFFParseBase);
  201.  
  202.     if (rdargs)
  203.         FreeArgs(rdargs);
  204.  
  205.     Close(StdErr);
  206.     return 0;
  207. }
  208.